tool

extends Control

var firstItem:TreeItem

onready var classTree = $"HSplit/classTree"
onready var funcsTree = $"HSplit/VSplitContainer/funsTree"
onready var dscTxt = $"HSplit/VSplitContainer/dscTxt"


func _ready():
	# 获取可用的类的名称
	var class_list = ClassDB.get_class_list()
	var i = 0
	# 添加到类名列表
	for clas in class_list:
		if ClassDB.can_instance(clas):
			var item = classTree.create_item()
			if i == 0:
				firstItem = item
				
			item.set_text(0,clas)
			item.set_icon(0,get_icon("Object","EditorIcons"))
			if i<1:
				i+=1

	firstItem.select(0)
	pass



func _on_classTree_item_selected():
	var sel_class_item:TreeItem = classTree.get_selected()
	var sel_class_name = sel_class_item.get_text(0) # 当前选中类名
	
	
	var a = ClassDB.get_parent_class(sel_class_name) # 父类
	# 成员列表
	var constlist = ClassDB.class_get_integer_constant_list(sel_class_name,true)
	var proplist = ClassDB.class_get_property_list(sel_class_name,true)
	var funclist = ClassDB.class_get_method_list(sel_class_name,true)
	var signallist = ClassDB.class_get_signal_list(sel_class_name,true)
	
	
	
	
#	print(funclist)
	funcsTree.clear()
	funcsTree.set_column_title(0,"成员")
	funcsTree.set_column_min_width(0,2)
	funcsTree
	funcsTree.set_column_title(1,"类型")
	funcsTree.set_column_titles_visible(true)
#	print(constlist)
	# 加载常量
	for cons in constlist:
		var item = funcsTree.create_item()
		item.set_text(0,cons)
		var consVal = str(ClassDB.class_get_integer_constant(sel_class_name,cons))
		item.set_suffix(0," = " + consVal)
		item.set_text(1,"常量")
		item.set_icon(0,get_icon("MemberConstant","EditorIcons"))
#	print(proplist)
	# 加载属性
	for prop in proplist:
		var item = funcsTree.create_item()
		item.set_text(0,prop["name"])
		item.set_suffix(0,":" + typeName(prop["type"]))
		item.set_icon(0,get_icon("MemberProperty","EditorIcons"))
		item.add_user_signal("is_prop")
	
	# 加载方法
	for fuc in funclist:
		var item = funcsTree.create_item()
		var args = fuc["args"]
		var args_str = ""
		for arg in args:
			args_str += arg["name"] + ":" + str(typeName(arg["type"])) + ","
		if args_str.ends_with(","):
			args_str = args_str.substr(0,args_str.length()-1)
		item.set_text(0,fuc["name"])
		item.set_suffix(0,"(%s)" % args_str + " -> " + typeName(fuc["return"]["type"]))
		item.set_icon(0,get_icon("MemberMethod","EditorIcons"))
		item.add_user_signal("is_func")
	print(signallist)
	# 加载信号
	for sig in signallist:
		var item = funcsTree.create_item()
		item.set_text(0,sig["name"])
		item.set_icon(0,get_icon("MemberSignal","EditorIcons"))
		item.add_user_signal("is_signal")
	pass # Replace with function body.

# 返回对应类名的子节点
func find_child_as_class(node:Node,className:String):
	var children = node.get_children()
	for child in children:
		if child.is_class(className):
			return child

func _on_funsTree_item_selected():
	var sel_class_item:TreeItem = classTree.get_selected()
	var sel_menber_item:TreeItem = funcsTree.get_selected()
	
	var classTypeName = sel_class_item.get_text(0) # 当前选中类名
	var menberName = sel_menber_item.get_text(0) # 当前选中成员名
	var menberType = sel_menber_item.get_text(1) # 当前选中成员类型
	print(menberName)
	
	# 显示“搜索帮助对话框”
	var plug = EditorPlugin.new()
	var script_editor = plug.get_editor_interface().get_script_editor()
	var serchHelpBtn:ToolButton = script_editor.get_child(0).get_child(0).get_child(11)
	serchHelpBtn.emit_signal("pressed")
	# 修改搜索关键词为当前成员名
	var serchHelpDlg = find_child_as_class(script_editor,"EditorHelpSearch")
	var serchHelpTxt:LineEdit = serchHelpDlg.get_child(3).get_child(0).get_child(0)
	var serchHelpOKBtn:Button = serchHelpDlg.get_child(2).get_child(1)
	
	serchHelpTxt.text = menberName
	serchHelpTxt.emit_signal("text_changed",menberName)
	serchHelpOKBtn.emit_signal("pressed")
	serchHelpOKBtn.emit_signal("button_down")
	
	pass # Replace with function body.


# 返回数字所代表的类型的名称字符串
func typeName(typeNum:int) -> String:
	var typeNameArr=[
		"null",
		"bool","int","float",
		"String","Vector2","Rect2",
		"Vector3","Transform2D","Plane",
		"Quat","AABB","Basis",
		"Transform","Color","NodePath",
		"RID","Object","Dictionary",
		"Array","PoolByteArray","PoolIntArray",
		"PoolRealArray","PoolStringArray","PoolVector2Array",
		"PoolVector3Array","PoolColorArray"
	]
	return typeNameArr[typeNum]


func _on_funsTree_item_double_clicked():
	
	
	pass # Replace with function body.
